package entity.weapons;

import java.awt.Container;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StreamTokenizer;
import java.util.LinkedList;

import javax.swing.JFileChooser;

import editor.weapon.ActionScript;
import editor.weapon.Function;

import loading.ImageLoader;
import math.Vector2D;

public class Weapon extends Item
{
	private int damage;
	
	public static Weapon load( String pathname )
	{
		File f = new File( pathname );
		try
		{
			FileReader stream = new FileReader( f );
			BufferedReader buf = new BufferedReader( stream );
			
			String name = buf.readLine();
			ImageLoader icon = new ImageLoader( buf.readLine() );
			ImageLoader img = new ImageLoader( buf.readLine() );
			int damage = Integer.valueOf( buf.readLine() );
			
			stream.close();
			
			return new Weapon( name, icon, img, damage );
		} 
		catch ( FileNotFoundException e )
		{
			e.printStackTrace();
		} 
		catch ( IOException e )
		{
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static Weapon load( Container frame )
	{
		JFileChooser fc = new JFileChooser();
		fc.setCurrentDirectory( new File( "C:/Documents and Settings/Owner/Desktop/Projects/TopDown/Weapons" ) );
		int returnVal = fc.showOpenDialog( frame );
		
		if( returnVal == JFileChooser.APPROVE_OPTION )
		{
			return load( fc.getSelectedFile().getAbsolutePath() );
		}
			
		return null;
	}
	
	public Weapon( String name, Vector2D position, ImageLoader icon, ImageLoader image, int damage )
	{
		super( name, position, icon, image );
		
		this.damage = damage;
	}
	
	public Weapon( String name, ImageLoader icon, ImageLoader image, int damage )
	{
		super( name, new Vector2D( 0, 0 ), icon, image );
		
		this.damage = damage;
	}
	
	public int getDamage()
	{
		return damage;
	}
	
	public void save()
	{
		File f = new File( "Weapons/" + super.getName() + ".txt" );
		
		try 
		{ 
			f.createNewFile();
			BufferedWriter buf = new BufferedWriter( new FileWriter( f.getAbsolutePath() ) );
			
			String s = "";
			s += super.getName() + "\n";
			s += super.getIcon().getFilename() + "\n";
			s += super.getImage().getFilename() + "\n";
			s += this.getDamage() + "\n";
			
			buf.write( s );
			
			buf.close();
		} 
		catch ( FileNotFoundException err ) 
		{
			err.printStackTrace();
		} 
		catch ( IOException err ) 
		{
			err.printStackTrace();
		}
	}
}
